using System;
using System.Collections.Generic;
using UnityEngine;

public class PlayerResourcesManager : SingletonMono<PlayerResourcesManager>
{
    public IntVariable player1Resources;
    public IntVariable player2Resources;
    
    public List<GameObject> player1ResourcesList;
    public List<GameObject> player2ResourcesList;

    public int Player1Resources
    {
        get => player1Resources.currentValue;
        set => player1Resources.SetValue(value);
    }

    public int Player2Resources
    {
        get => player2Resources.currentValue;
        set => player2Resources.SetValue(value);
    }

    
        
    

    private void OnEnable()
    {
        EventCenter.GetInstance().AddEventListener<ChessSide,int>(nameof(AddResources), AddResources);
        EventCenter.GetInstance().AddEventListener<ChessSide,int>(nameof(ReduceResources), ReduceResources);
        EventCenter.GetInstance().AddEventListener<int>("Player1ResourceValueChange", Player1ResourceValueChange);
        EventCenter.GetInstance().AddEventListener<int>("Player2ResourceValueChange", Player2ResourceValueChange);
    }

    

    private void OnDisable()
    {
        EventCenter.GetInstance().RemoveEventListener<ChessSide,int>("AddResources", AddResources);
        EventCenter.GetInstance().RemoveEventListener<ChessSide,int>("ReduceResources", ReduceResources);
        EventCenter.GetInstance().RemoveEventListener<int>("Player1ResourceValueChange", Player1ResourceValueChange);
        EventCenter.GetInstance().RemoveEventListener<int>("Player2ResourceValueChange", Player2ResourceValueChange);
    }

    public void AddResources(ChessSide side, int amount = 1)
    {
        if (side == ChessSide.Player1)
        {
            if ((Player1Resources + amount) < player1Resources.maxValue)
            {
                Player1Resources += amount;
            }
            else
            {
                Player1Resources = player1Resources.maxValue;
            }
        }
        else
        {
            if ((Player2Resources + amount) < player2Resources.maxValue)
            {
                Player2Resources+=amount;
            }
            else
            {
                Player2Resources = player2Resources.maxValue;
            }
        }
    }
    
    
    public void ReduceResources(ChessSide side, int amount)
    {
        if (side == ChessSide.Player1)
        {
            if (Player1Resources - amount >= 0)
            {
                Player1Resources -= amount;
            }
        }
        else
        {
            if (Player2Resources - amount >= 0)
            {
                Player2Resources -= amount;
            }
        }
    }
    
    private void Player2ResourceValueChange(int value)
    {
        for (var i = 0; i < value; i++)
        {
            player2ResourcesList[i].SetActive(true);
        }
        for (var i = value; i < player2ResourcesList.Count; i++)
        {
            player2ResourcesList[i].SetActive(false);
        }
    }

    private void Player1ResourceValueChange(int value)
    {
        for (var i = 0; i < value; i++)
        {
            player1ResourcesList[i].SetActive(true);
        }
        for (var i = value; i < player1ResourcesList.Count; i++)
        {
            player1ResourcesList[i].SetActive(false);
        }
    }

    public void GameStart()
    {
        Player1Resources = 10;
        Player2Resources = 10;
    }
    public void GameOver()
    {
        Player1Resources = 0;
        Player2Resources = 0;
    }
}
